package Gr

import "fmt"

var (
	getNestedTableSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT `TABLE_NAME` from %v where owner = '%s' %v", o, s, t)
	}
	getTableNameSql = func(o, s, t string) string {
		return fmt.Sprintf("select table_NAME as `tableName` from %v where TABLE_TYPE = 'BASE TABLE'  AND table_schema ='%v' %v", o, s, t)
	}
	getSeqNameSql = func(o, s, t string) string {
		return fmt.Sprintf("select name from %v where  db ='%v' %v", o, s, getLikeTable(t))
	}
	getTableExistenceSql = func(o, s, t string) string {
		return fmt.Sprintf("select table_name from %v where TABLE_TYPE = 'BASE TABLE'  AND table_schema ='%v'  and table_NAME = '%v'", o, s, t)
	}
	getSeqExistenceSql = func(o, s, t string) string {
		return fmt.Sprintf("select name from %v where db = '%v' AND name ='%v'", o, s, t)
	}
	getSchemaNameSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT SCHEMA_NAME  FROM %v WHERE SCHEMA_NAME = '%v'", o, s)
	}
	getTableCommentSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT TABLE_COMMENT as `comment` FROM %v WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME='%v'", o, s, t)
	}
	getColumnCommentSql = func(o, s, t string) string {
		return fmt.Sprintf("select COLUMN_NAME as `columnName`,COLUMN_COMMENT as `columnComment` from %v where table_schema = '%v' and table_name in '%v' order by ORDINAL_POSITION", o, s,t)
	}
	getTableCharacterSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT REGEXP_SUBSTR(TABLE_COLLATION, '^[^_]+') AS `character` FROM %v WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME='%v'", o, s, t)
	}
	getTableCollationSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT TABLE_COLLATION as `collation` FROM %v WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME='%v'", o, s, t)
	}
	getTableConstraintsSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT tc.CONSTRAINT_NAME as `name`,cc.CHECK_CLAUSE as `body` FROM %v AS tc JOIN information_schema.CHECK_CONSTRAINTS AS cc ON tc.CONSTRAINT_NAME = cc.CONSTRAINT_NAME WHERE tc.TABLE_SCHEMA = '%v' AND tc.TABLE_NAME = '%v' AND tc.CONSTRAINT_TYPE = 'CHECK'", o, s, t)
	}
	getSchemaDefaultCharacterSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT DEFAULT_CHARACTER_SET_NAME as `character` FROM %v where SCHEMA_NAME='%v'", o, s)
	}
	getSeqBodySql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT `db` AS `schema`,`name` AS `name`,`minvalue` AS `minValue`,`maxvalue` AS `maxValue`,`increment` AS `incrementBy`,`cycle_flag` AS `cycleFlag`,`order_flag` AS `orderFlag`,`cache_num` AS `cacheSize`,`start_with` AS `startValue` FROM %v WHERE `db`='%v' and `name`='%v'", o, s, t)
	}
	getGIpkSql = func(o, s, t string) string {
		return fmt.Sprintf("select VARIABLE_VALUE as `value` from %v where Variable_name='%v'", o, s)
	}
	getLobColumnSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT `COLUMN_NAME` as `columnName`,`DATA_TYPE` as `columnType` FROM %v WHERE TABLE_SCHEMA = '%v'  AND TABLE_NAME = '%v' AND DATA_TYPE IN ('tinyblob', 'blob', 'mediumblob', 'longblob')",o,s,t)
	}
)

var (
	createSeqBodySql = func(s, t, mi, ma, in, st, ca, or, cy string) string {
		return fmt.Sprintf("use %v; \n"+
			"CREATE SEQUENCE `%v`.`%v` \n"+
			"\t MINVALUE %v \n"+
			"\t MAXVALUE %v \n"+
			"\t INCREMENT BY %v \n"+
			"\t START WITH %v \n "+
			"\t %v %v %v; \n", s, s, t, mi, ma, in, st, ca, or, cy)
	}
	dropSeqSql = func(s, t string) string {
		return fmt.Sprintf("DROP SEQUENCE IF EXISTS `%v`.`%v`", s, t)
	}
	dropTableSql = func(s, t string) string {
		return fmt.Sprintf("DROP TABLE IF EXISTS `%v`.`%v`", s, t)
	}
	renameTableSql = func(s, t,ss,tt string) string {
		return fmt.Sprintf("RENAME TABLE `%v`.`%v` TO `%v`.`%v`", s, t,ss,tt)
	}
	createDatabaseSql = func(s string) string {
		return fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%v`", s)
	}
	likeTableSql = func(s, t, ss,tt string) string {
		return fmt.Sprintf("CREATE TABLE `%v`.`%v` LIKE `%v`.`%v`", s, t, ss, t)
	}
	truncateTableSql = func(s, t string) string {
		return fmt.Sprintf("TRUNCATE TABLE `%v`.`%v`", s, t)
	}
	truncatePartSql = func(s, t, p string) string {
		return fmt.Sprintf("ALTER TABLE `%v`.`%v` TRUNCATE PARTITION `%v`", s, t, p)
	}
	useSchemaSql = func(s string) string {
		return fmt.Sprintf("USE `%v`", s)
	}
	analyzeTableSql = func(t string) string {
		return fmt.Sprintf("ANALYZE TABLE `%v`", t)
	}
)
